/*
  Copyright (C) 2013 Jolla Ltd.
  Contact: Thomas Perl <thomas.perl@jollamobile.com>
  All rights reserved.

  You may use this file under the terms of BSD license as follows:

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are met:
    * Redistributions of source code must retain the above copyright
      notice, this list of conditions and the following disclaimer.
    * Redistributions in binary form must reproduce the above copyright
      notice, this list of conditions and the following disclaimer in the
      documentation and/or other materials provided with the distribution.
    * Neither the name of the Jolla Ltd nor the
      names of its contributors may be used to endorse or promote products
      derived from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR
  ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
import QtQuick 2.0
import "appdetail.js" as JS
import Sailfish.Silica 1.0
Page{
    id:showappdetail
    property int operationType: PageStackAction.Animated
    property var id
    property string author
    property string downurl
    SilicaFlickable {
        anchors.fill: parent
        Component.onCompleted: {
            JS.loadappdetail(id);
            JS.searchbyauthor(author,id);
        }
        ListModel {  id:fooddetailModel }
        ListModel{id:screenShotModel}
        ListModel{id:moreAppsModel}

        PullDownMenu {
            MenuItem {
                text: "安装"
                onClicked: {
                    Qt.openUrlExternally(downurl)
                }
            }
        }
    PageHeader {
        id:header
        title: "应用详情"
    }
    Item {
        id:root
        width: parent.width
        height: 400
        //height: showlist.height
        y:100
        Row {
            id:progress
            spacing: Theme.paddingLarge
            anchors.horizontalCenter: parent.horizontalCenter

            BusyIndicator {
                running: true
                size: BusyIndicatorSize.Large
                anchors.verticalCenter: parent.verticalCenter
            }
        }

        SilicaListView{
            id:view
            anchors.fill:parent
            model : fooddetailModel
            clip: true
            delegate:
                BackgroundItem{
                id:showlist
                height: childrenRect.height
                Label{
                    id:imgid
                    Image{
                        id:appicon
                        fillMode: Image.Stretch;
                        x:implicitWidth/2
                        width:  implicitWidth*4/3
                        height: implicitHeight*4/3
                        source: icon
                    }
                        anchors {
                            left: parent.left
                        }
                }
                Label{
                    id:versionid
                    x:appicon.width/2
                    y:appicon.height
                    text:"<font size='2' > 版本:</font><font size='1' >"+version+"</font><br/>"+
                            "<font size='2' > 人气:</font><font size='1' >"+views+"</font><br/>"
                    font.pixelSize: Theme.fontSizeExtraSmall
                    truncationMode: TruncationMode.Fade
//                    anchors {
//                        left: imgid.right
//                        top:imgid.bottom
//                    }
                }
                Label{
                    id:infomess
                    x:appicon.width+(parent.width-appicon.width)/2
                    text:
                         "<font size='2' > 下载:</font><font size='1' >"+downloads+"</font><br/>"+
                         "<font size='2' > 容量:</font><font size='1' >"+Format.formatFileSize(size)+"</font><br/>"+
                         "<font size='2' > 作者:</font><font size='1' >"+author+"</font><br/>"+
                         "<font size='2' > 类型:</font><font size='1' >"+category+"</font><br/>"+
                         "<font size='2' > 收录:</font><font size='1' >"+dateline+"</font><br/>"
                    font.pixelSize: Theme.fontSizeExtraSmall
                    horizontalAlignment: Text.AlignRight
                    anchors {
                      bottom:versionid.bottom
                    }
                }

                Label{
                    id:summaryid
                    x:appicon.width+20
                    width: parent.width-50
                    text:"<font size='1' >"+summary+"</font>"
                    font.pixelSize: Theme.fontSizeExtraSmall*4/3
                    wrapMode: Text.WordWrap
                    //horizontalAlignment: Text.AlignRight
                    anchors {
                        top:versionid.bottom
                        left:parent.left
                    }

                }
                VerticalScrollDecorator {}

            }
        }
    }

            Item{
                id:screenshots
                height:sshotid.height
                anchors {
                    left:parent.left
                    top:root.bottom
                }
                Image{
                    id:sshotid
                    source: icon
                    fillMode: Image.Stretch;
                    x:implicitWidth/2
                    width:  implicitWidth*4/3
                    height: implicitHeight*4/3
                }
                Separator {
                    width:parent.width;
                    color: Theme.highlightColor
                }
        }


            SectionHeader {
                id:ttttt
                text:"相关应用"
                height: Theme.itemSizeMedium
                anchors {
                    left:parent.left
                    top:screenshots.bottom
                }
                Separator {
                    width:parent.width;
                    color: Theme.highlightColor
                }
             }
            Item{
                 id:moreapp
                 width: root.width
                 height: 250
                 y:root.height+screenshots.height+50
                 anchors {
                     left:parent.left
                     top:ttttt.bottom
                 }


            SilicaGridView {
                id: gridView
                y:root.height+screenshots.height+others.height+50
                height: childrenRect.height
                model: moreAppsModel
                anchors.fill: parent
                currentIndex: -1
                cellWidth: gridView.width / 3
                cellHeight: cellWidth
                GridView.onAdd: AddAnimation {
                    target: rectangle
                }
                GridView.onRemove: RemoveAnimation {
                    target: rectangle
                }
                delegate: BackgroundItem {
                    id: rectangle
                    width: gridView.cellWidth
                    height: gridView.cellHeight
                Label{
                    id: label
                    x: morepic.width/2
                    text:moreappname+"<br/><font size='2' > 人气:</font><font size='1' >"+moreviews+"</font>"
                    font.pixelSize: Theme.fontSizeExtraSmall
                    horizontalAlignment: Text.AlignRight
                }
                Label{
                    id:moreimgid
                    Image{
                        id:morepic
                        x:implicitWidth/2
                        y:label.height
                        fillMode: Image.Stretch;
                        width:  implicitWidth
                        height: implicitHeight
                        source: moreicon
                    }
                }
                onClicked :{
                    pageStack.replace(Qt.resolvedUrl("AppDetail.qml"),{
                                          "id":moreappid,
                                          "author":author
                                      })
                }

                }

                VerticalScrollDecorator {}

            }
        }
}
    }
 
